<?php
include 'include/common.php';
$core = new core();
$message_error = '';
if (isset($_GET["arg"])) {
    $decode_arg = base64_decode($_GET["arg"]);
    $array_decode_arg = explode("-", $decode_arg);
    if (count($array_decode_arg) == 3) {
        $event_id = intval($array_decode_arg[0]);
        $guest_id = intval($array_decode_arg[1]);
        $attend = intval($array_decode_arg[2]);
        if($attend !==0 && $attend !==1 && $attend !==2){
            $attend=0;
        }
        $data = $core->db->get_row("SELECT * FROM guest_event WHERE `guest_id`='$guest_id' AND event_id='$event_id'");

        if (!$data) {
            $message_error = 'The requested URL was not found';
        } else {
            $data = $core->db->get_row("SELECT * FROM events WHERE event_id=$event_id");
            if ($data) {
                if ($data->end_time == $data->start_date) {
                    $end_time = "";
                } else {
                    $end_time = date('H:i A', strtotime($data->end_time));
                }
                $title = $data->title;
                $start_date = date('m/d/Y', strtotime($data->start_date));
                $start_time = date('H:i A', strtotime($data->start_time));
                $location = $data->location;
                $description = $data->description;
                //update state invite with each guest
                $core->db->query('UPDATE guest_event
                        SET attend ='.$attend.',is_invited = 1
                        WHERE guest_id =' . $guest_id . ' AND event_id =' . $event_id);
            } else {
                $message_error = 'Event does not exist';
            }
        }
    } else {
        $message_error = 'Oops, something went wrong. Please try again.';
    }
} else {
    $message_error = 'Oops, something went wrong. Please try again.';
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
        <link rel="stylesheet" type="text/css" href="Client/www/bootstrap/css/bootstrap.css" />
        <link rel="stylesheet" type="text/css" href="Client/www/bootstrap/css/bootstrap.min.css" />
        <link rel="stylesheet" type="text/css" href="Client/www/bootstrap/css/bootstrap-responsive.css" />
        <link rel="stylesheet" type="text/css" href="Client/www/bootstrap/css/bootstrap-responsive.min.css" />
        <link rel="stylesheet" type="text/css" href="Client/www/bootstrap/customize/css/style.css" />
        <title>Reply invitation</title>  
         <script type="text/javascript">
           
        </script>
    </head>
    <body>
        <div class="container-fluid">
            <form class="form-request form-reply">
                <h2 class="form-signin-heading">Reply Invitation</h2>
                <div class="row-fluid show-grid">
                    <div class="span12 alert alert-error" id="error-msg" style="<?php if ($message_error == "") echo "display:none";
else echo "display:block"; ?>">  

<?php echo $message_error; ?>
                    </div>
                </div>
                <div class="row-fluid show-grid">
                    <div class="span12 alert alert-success">  
                        <a class="close" data-dismiss="alert" href="#">&times;</a>
                        Your response has been saved 
                    </div>
                </div>
                <div class="row-fluid show-grid">
                    <div class="alert alert-info">  
                        <a class="close" data-dismiss="alert">×</a>  
                        <?php
                        if ($attend == 0) {
                            echo "Yes, I might go.";
                        } elseif ($attend == 1) {
                            echo "Yes, I'm going.";
                        } elseif ($attend == 2) {
                            echo "No, I'm not going.";
                        }
                        ?>
                    </div>  
                </div>
                <div class="row-fluid show-grid">
                    <div class="span3">Event name :</div>
                    <div class="span9"><?php echo $title; ?></div>
                </div>
                <?php
                if($description!=""){
                ?>
                <div class="row-fluid show-grid">
                    <div class="span3">Event description :</div>
                    <div class="span9">
                        <?php echo $description; ?>
                    </div>
                </div>
                <?php
                }
                ?>
                <div class="row-fluid show-grid" style="margin-top: 10px;">
                    <div class="span3">Event location :</div>
                    <div class="span9"><?php echo $location; ?></div>
                </div>
                <div class="row-fluid show-grid">
                    <div class="span3">Event time :</div>
                    <?php
                    if($end_time!=""){
                    ?>
                        <div class="span9"><?php echo $start_date . " " . $start_time . " - " . $end_time; ?></div>
                    <?php
                    }else{
                    ?>
                        <div class="span9"><?php echo $start_date . " " . $start_time ; ?></div>
                    <?php
                    }
                    ?>
                </div>
                <div class="row-fluid show-grid">
                    <div class="span12"><a href="http://<?php  echo $_SERVER['SERVER_NAME'];?>">Learn More about Wedding Helper App</a></div>
                    
                </div>
            </form>
        </div>
        <script type="text/javascript" src="Client/www/js/jquery-1.9.1.min.js"></script>  
        <script type="text/javascript" src="Client/www/bootstrap/js/bootstrap.js"></script>  
        <script type="text/javascript" src="Client/www/bootstrap/js/bootstrap.min.js"></script> 
        <script type="text/javascript" src="Client/www/bootstrap/js/customize/js/util.js"></script> 
    </body>
</html>
